var express = require('express');
var router = express.Router();
const mongodb= require('../components/mongo');

const multer = require('multer');
const uploader = multer({dest:'uploads/'});

/* GET home page. */
router.get('/',async function(req, res, next) {
   console.log(req.query);
   var m_type = req.query.type;
   var m_mode = req.query.mode;
   
   if( m_type == undefined || m_type <= 0 || m_type > 5){
	   //type == 0 or not exist
	   const result = await mongodb.find("Article",{});
	   if(result.length>0){
	   	   res.json({
	   		   errno:0,
	   		   errmsg:"",
	   		   data:{
	   			   res:true,
	   			   articles:result
	   		   },
	   		   });
	   }else{
	   	   res.json({
	   	      errno:0,
	   	      errmsg:"",
	   	      data:{res:false},
	   	      });
	   }
   }else{
	   // with type = 1,2,3,4,5
	   const result = await mongodb.find("Article",{type:m_type});
	   if(result.length>0){
	   	   res.json({
	   		   errno:0,
	   		   errmsg:"",
	   		   data:{
	   			   res:true,
	   			   articles:result
				},
	   		   });
	   }else{
	   	   res.json({
	   	      errno:0,
	   	      errmsg:"",
	   	      data:{res:false},
	   	      });
	   }
   }
   
});

var sortByMode = function(article,mode){
	//if(mode=="time")
}


module.exports = router;